package logic

import (
	"context"

	"hm-zero-mall/apps/address/pb"
	"hm-zero-mall/apps/address/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAddressListByUserIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAddressListByUserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAddressListByUserIdLogic {
	return &GetAddressListByUserIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetAddressListByUserId 根据用户id获取地址列表
func (l *GetAddressListByUserIdLogic) GetAddressListByUserId(in *pb.FindAddressByUserIdReq) (*pb.AddressListResp, error) {
	addressList, err := l.svcCtx.AddressModel.FindList(l.ctx, in.Uid, nil)
	if err != nil {
		return nil, err
	}

	var addresses []*pb.AddressInfo
	for _, address := range addressList {
		addresses = append(addresses, &pb.AddressInfo{
			Id:        address.Id,
			Uid:       address.UserId,
			Province:  address.Province,
			City:      address.City,
			Town:      address.Town,
			Mobile:    address.Mobile,
			Street:    address.Street,
			Contact:   address.Contact,
			Notes:     address.Notes,
			IsDefault: address.IsDefault,
		})
	}
	return &pb.AddressListResp{AddressList: addresses}, nil
}
